import store from '@/store/index.js'
const baseURL = 'https://api-hmugo-web.itheima.net/api/public/v1'
const request = ({
	url,
	method = 'get',
	data = {}
}) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: url,
			method: method,
			data: data,
			success: (res) => {
				resolve(res.data)
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

uni.addInterceptor('request', {
	invoke(args) {
		// request 触发前拼接 url 
		const token = store.state.my_user.token
		if (token) {
			args.header = {
				Authorization: token
			}
		}
		args.url = baseURL + args.url
	},
	success(args) {
		// 请求成功后，修改code值为1
		// args.data.code = 1
		console.log(args)
	},
	fail(err) {
		// console.log('interceptor-fail', err)
	},
	complete(res) {
		// console.log('interceptor-complete', res)
	}
})

export default request
